package org.zxl.springbootdemo.quartz.config;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.zxl.springbootdemo.quartz.task.MyTask1;
import org.zxl.springbootdemo.quartz.task.MyTask2;

@Configuration
public class TaskConfig {

    @Bean
    public JobDetail jobDetailMyTask1(){
        return JobBuilder.newJob(MyTask1.class).withIdentity("jobDetailMyTask1").storeDurably().build();
    }


    @Bean
    public JobDetail jobDetailMyTask2(){
        return JobBuilder.newJob(MyTask2.class).withIdentity("jobDetailMyTask2").storeDurably().build();
    }

    private static final String myTask1Cron = "59/10 * * * * ? *";

    @Bean
    public Trigger myTask1Trigger(@Autowired JobDetail jobDetailMyTask1){
//        JobDetail myTask1 = JobBuilder.newJob(MyTask1.class).withIdentity("myTask1").storeDurably().build();
        CronScheduleBuilder scheduleBuilder =CronScheduleBuilder.cronSchedule(myTask1Cron);
        Trigger trigger = TriggerBuilder.newTrigger().forJob(jobDetailMyTask1)
                .withIdentity("jobDetailMyTask1")
                .withSchedule(scheduleBuilder)
                .build();
        return trigger;
    }


    private static final String myTask2Cron = "30/10 * * * * ? *";
    @Bean
    public Trigger myTask2Trigger(@Autowired JobDetail jobDetailMyTask2){
//        JobDetail myTask2 = JobBuilder.newJob(MyTask2.class).withIdentity("myTask2").storeDurably().build();
        CronScheduleBuilder scheduleBuilder =CronScheduleBuilder.cronSchedule(myTask2Cron);
        Trigger trigger = TriggerBuilder.newTrigger().forJob(jobDetailMyTask2)
                .withIdentity("jobDetailMyTask2")
                .withSchedule(scheduleBuilder)
                .build();
        return trigger;
    }
}
